package com.ship.dispatch.controller.report;

import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.dispatch.bean.report.ReportItem;
import com.ship.dispatch.bean.report.SpWorkbenchMenu;
import com.ship.dispatch.model.dto.report.*;
import com.ship.dispatch.service.report.ReportItemService;
import com.ship.dispatch.service.report.SpWorkbenchMenuService;
import com.ship.dispatch.vo.report.DataContentByTypeVo;
import com.ship.dispatch.vo.report.ReportInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/report/")
@Api(value = "report", tags = "数据看板")
public class ReportController {

    @Autowired
    private ReportItemService reportItemService;
    @Autowired
    private SpWorkbenchMenuService workbenchMenuService;

    @ApiOperation(value = "获取个人工作台快捷菜单")
    @GetMapping("getWorkbenchMenu")
    public JsonResult getWorkbenchMenu() {
        SpWorkbenchMenu menu = workbenchMenuService.getWorkbenchMenu();
        return JsonResult.success(menu);
    }

    @ApiOperation(value = "保存工作台快捷菜单表")
    @PostMapping("saveWorkbenchMenu")
    @Log(title = "保存工作台快捷菜单表", businessType = BusinessType.INSERT)
    public JsonResult saveWorkbenchMenu(@RequestBody WorkbenchMenuDto workbenchMenuDto) {
        workbenchMenuService.saveWorkbenchMenu(workbenchMenuDto.getWorkbenchContent());
        return JsonResult.success();
    }

    @ApiOperation(value = "获取所有数据源信息")
    @GetMapping("getDataSourceInfo")
    public JsonResult getDataSourceInfo(@RequestParam(required = false) String sourceName) {
        List<DataContentByTypeVo> dataContentByTypeVos = reportItemService.getDataSourceInfo(sourceName);
        return JsonResult.success(dataContentByTypeVos);
    }

    @ApiOperation(value = "根据数据源名称信息获取对应图表类型数据")
    @GetMapping("getTableTypeBySource")
    public JsonResult getTableTypeBySource(@RequestParam(required = false) String sourceName) {
        List<DataContentByTypeVo> dataContentByTypeVos = reportItemService.getTableTypeBySource(sourceName);
        return JsonResult.success(dataContentByTypeVos);
    }



    /**
     * 根据不同展示类型选择不同接口
     * @param typeId  1：数字，2：柱状，3：饼状，4：堆积图，5:双柱状图，6：折线图，7：表格
     * @return
     */
    @ApiOperation(value = "根据不同展示类型选择不同接口")
    @GetMapping("getDataContentByType")
    public JsonResult getDataContentByType(@RequestParam Integer typeId,@RequestParam(required = false) String portName) {
        List<DataContentByTypeVo> dataContentByTypeVos = reportItemService.getDataContentByType(typeId,portName);
        return JsonResult.success(dataContentByTypeVos);
    }

    @ApiOperation(value = "保存统计报表渲染表")
    @PostMapping("saveReportItem")
    @Log(title = "保存统计报表渲染表", businessType = BusinessType.INSERT)
    public JsonResult saveReportItem(@RequestBody ReportItem reportItem) {
        return JsonResult.success(reportItemService.saveReportItem(reportItem));
    }

    @ApiOperation(value = "查询统计报表渲染表")
    @GetMapping("selectReportItems")
    public JsonResult selectReportItems() {
        return JsonResult.success(reportItemService.selectReportItems());
    }

    @ApiOperation(value = "添加图表接口")
    @PostMapping("saveReportInfo")
    @Log(title = "添加图表接口", businessType = BusinessType.INSERT)
    public JsonResult saveReportInfo(@RequestBody ReportItemDto reportItemDtos) {
        reportItemService.saveReportInfo(reportItemDtos);
        return JsonResult.success();
    }

    @ApiOperation(value = "删除图表接口")
    @DeleteMapping("removeReportInfo/{id}")
    @Log(title = "删除图表接口", businessType = BusinessType.DELETE)
    public JsonResult removeReportInfo(@PathVariable Long id) {
        reportItemService.removeReportInfo(id);
        return JsonResult.success();
    }

    @ApiOperation(value = "查看图表接口")
    @PostMapping("selectReportInfos")
    public JsonResult selectReportInfos(@RequestBody ReportInfoDto reportInfoDto) {
        return JsonResult.success(reportItemService.selectReportInfos(reportInfoDto));
    }

    @ApiOperation(value = "更新图表接口(拖动更新)")
    @PostMapping("updateReportInfo")
    @Log(title = "更新图表接口(拖动更新)", businessType = BusinessType.UPDATE)
    public JsonResult updateReportInfo(@RequestBody List<UpdateReportInfoDto> updateReportInfoDtos) {
        reportItemService.updateReportInfo(updateReportInfoDtos);
        return JsonResult.success();
    }

    @ApiOperation(value = "根据数据源获取相关数据")
    @PostMapping("getDataContentByKey")
    public JsonResult getDataContentByKey(@RequestBody getReportDataInfoDto dto) throws ParseException {
        if(dto.getType() == null || dto.getDateType() == null){
            return JsonResult.failed("参数信息异常！");
        }
        return JsonResult.success(reportItemService.getDataContentByKey(dto));
    }

    @ApiOperation(value = "更新统计渲染表")
    @PostMapping("updateReportItem")
    @Log(title = "更新统计渲染表", businessType = BusinessType.UPDATE)
    public JsonResult updateReportItem(@RequestBody ReportItemUpdateDto reportItemUpdateDto) {
        return JsonResult.success(reportItemService.updateReportItem(reportItemUpdateDto));
    }

    @ApiOperation(value = "推送工作台")
    @PostMapping("saveWorkbench")
    @Log(title = "推送工作台", businessType = BusinessType.INSERT)
    public JsonResult saveWorkbench(@RequestBody WorkbenchDto workbenchDto) {
        reportItemService.saveWorkbench(workbenchDto.getId());
        return JsonResult.success();
    }

    @ApiOperation(value = "查询工作台")
    @PostMapping("selectWorkbenchs")
    public JsonResult selectWorkbenchs(@RequestBody WorkbenchAllDto workbenchAllDto) throws ParseException {
        return JsonResult.success(reportItemService.selectWorkbenchs(workbenchAllDto));
    }

    @ApiOperation(value = "根据数据源id获取相关数据")
    @PostMapping("getDataContentById")
    public JsonResult getDataContentById(@RequestBody ReportInfoDataDto reportInfoDataDto) throws ParseException {
        return JsonResult.success(reportItemService.getDataContentById(reportInfoDataDto));
    }

    @ApiOperation(value = "删除工作台数据看板")
    @PostMapping("removeWorkbench")
    @Log(title = "删除工作台数据看板", businessType = BusinessType.DELETE)
    public JsonResult removeWorkbench(@RequestBody WorkbenchDto workbenchDto) {
        reportItemService.removeWorkbench(workbenchDto.getId());
        return JsonResult.success();
    }
}
